package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.adapters.JiePanListAdp
import hlhj.fhp.supreme.adapters.JiePanPagerAdp
import hlhj.fhp.supreme.fragment.MyJiePanListFgm
import hlhj.fhp.supreme.javabean.JiePanCatBean
import hlhj.fhp.supreme.javabean.RoomBea
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.toast
import hlhj.fhp.supreme.utils.JsonCallBack
import kotlinx.android.synthetic.main.myjiepan_aty.*

/**
 * Created by Never Fear   on 2018\7\17 0017.
Never More....
 */
class MyJiePanAty :BaseAty() {
    private val fgms=ArrayList<Fragment>()
    private lateinit var pagerAdp:JiePanPagerAdp
    private val tabs=ArrayList<JiePanCatBean.DataBean>()
    override fun getContentId(): Int {
        return R.layout.myjiepan_aty
    }

    override fun initView() {

        pagerAdp= JiePanPagerAdp(fgms,supportFragmentManager)
        viewPager.adapter=pagerAdp
        initTab()

    }

    private fun initTab() {
        tabLayout.tabMode=TabLayout.MODE_SCROLLABLE
        OkGo.post<JiePanCatBean>(Urls.BaseUrl+Urls.JIEPAN_CAT)
                .execute(object :JsonCallBack<JiePanCatBean>(this@MyJiePanAty){
                    override fun onSuccess(response: Response<JiePanCatBean>?) {
                        val jiePanCatBean = response?.body()
                        if (jiePanCatBean?.code==1){
                            tabs.addAll(jiePanCatBean?.data)
                            tabs.forEachIndexed { index, s ->
                                val newTab = tabLayout.newTab()
                                newTab.setText(s.name)
                                tabLayout.addTab(newTab)
                                val myJiePanListFgm = MyJiePanListFgm.getInstence(s.id.toString(),s.name)
                                fgms.add(myJiePanListFgm)
                                pagerAdp.notifyDataSetChanged()
                            }
                        }else{
                            this.toast(this@MyJiePanAty,jiePanCatBean?.msg!!)
                        }
                    }
                })



        tabLayout.setupWithViewPager(viewPager)
    }

    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
        btStart.setOnClickListener {
            val intent = Intent(this@MyJiePanAty, StartJiePanAty::class.java)
            startActivity(intent)
            /*OkGo.post<RoomBea>(Urls.BaseUrl+Urls.GET_ROOM)
                    .execute(object : JsonCallBack<RoomBea>(this@MyJiePanAty){
                        override fun onSuccess(response: Response<RoomBea>?) {
                            val bean = response?.body()
                            if (bean?.code==1){
                                if (bean?.data!=null){
                                    val intent = Intent(this@MyJiePanAty, JiePanDetailAty::class.java)
                                    intent.putExtra("roomId",bean?.data.chat_id)
                                    startActivity(intent)
                                }
                            }else{
                                val intent = Intent(this@MyJiePanAty, StartJiePanAty::class.java)
                                startActivity(intent)
                            }
                        }
                    })*/
        }
    }
}